var amapFile = require('@utils/amap-wx.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    markers: [],
    latitude: '',
    longitude: '',
    textData: {},
    MapContainer: []
  },
  doBook() {
    wx.navigateTo({
      url: '/pages/book/timeForm',
    })
  },
  sendGetSuggestBranchs(myLocation) {
    var arrs = []
    var markers = []
    var MapContainer = []
    const data = {
      "长沙": [{
        "id": 2,
        "name": "裕园中心",
        "region": "长沙",
        "banner": [
          "images/1.jpg",
        ],
        "address": "长沙市岳麓区学士街道（湖南中医院大学旁）裕园小区D1栋二单元二楼",
        "position": {
          "lat": "28.131534",
          "lon": "112.901759"
        }
      }]
    };
    //getSuggestBranchs({ api_token: wx.getStorageSync('token'), ...myLocation })

    for (const key in data) {
      data[key].forEach((item) => arrs.push(item))
    }
    console.log(data);
    // 
    markers = arrs.map((items) => {
      return {
        width: 22,
        height: 32,
        address: items.address,
        id: items.id,
        latitude: Number(items.position.lat),
        longitude: Number(items.position.lon),
        name: items.name
      }
    })

    MapContainer = arrs.map((items) => {
      return {
        id: items.id,
        image: "https://sixiao.lcol.top/" + items.banner[0],
        title: items.region,
        label: items.name,
        content: items.address
      }
    })

    this.setData({
      markers,
      MapContainer,
      latitude: markers[0].latitude,
      longitude: markers[0].longitude,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var AMapWX = new amapFile.AMapWX({
      key: '818dc464b70cfe7c506489643c84a4b5'
    });
    var self = this
    AMapWX.getRegeo({
      success: (data) => {
        self.sendGetSuggestBranchs({
          lat: data[0].latitude,
          lon: data[0].longitude
        })
      },
      fail: (err) => {
        console.log(err)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})